/**
 * 
 */   
package com.cares.core.common.web;  

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cares.core.base.controller.BaseController;
import com.cares.core.base.exception.ApplyException;
import com.cares.core.common.service.DicService;
import com.cares.core.utils.oConvertUtils;
import com.cares.entity.common.Dic;

/**   
 * @作者 Stone
 * @创建日期 2014-4-18 上午11:17:43
 * @版本 V1.0
 * @类说明
 */
@Controller
@RequestMapping(value = "/common/dic")
public class DicController extends BaseController {

	@Resource(name="dicService")
	private DicService dicService;
	
	private Map<String, String> map = new HashMap<String, String>();

	/**
	 * GET方法，主页面加载
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String init() {
		return "common/dic/dicTreeGrid";
	}
	
	/**
	 * 新增或更新字典项信息
	 */
	@RequestMapping(value = "updateDic")
	@ResponseBody
	public void updateDic(Dic dic, HttpServletRequest req, HttpServletResponse response) {
		try {
			map.put("STATE", "SUCCESS");
			dicService.saveOrUpdateDic(dic);
		} catch (Exception ex) {
			map.put("STATE", "ERROR");
			map.put("INFO", ex.getMessage());

		}
		super.outString(response, JSON.toJSONString(map));
	}
	
	/**
	 * 删除字典项及子字典项
	 */
	@RequestMapping(value = "del")
	@ResponseBody
	public void delete(Dic dic, HttpServletRequest req, HttpServletResponse response) {
		try {
			map.put("STATE", "SUCCESS");
			dicService.delete(dic.getId());
		} catch (Exception ex) {
			map.put("STATE", "ERROR");
			// map.put("INFO", ex.getMessage());

		}
		super.outString(response, JSON.toJSONString(map));
	}
	
	/**
	 * 查询字典列表信息
	 */
	@RequestMapping(value = "treeGridData")
	@ResponseBody
	public void treeGridData(Dic dic, HttpServletRequest req, HttpServletResponse response) {
		//Long nodeId = new Long("1");
		String id = oConvertUtils.getString(req.getParameter("id"));
		
		String dicCode = oConvertUtils.getString(req.getParameter("dicCode"));
		//List<Map<String, Object>> itemsList = dicService.getDicTree(nodeId);
		List<Map<String, Object>> itemsList = null;
		
		if (StringUtils.isNotEmpty(id)) {
			itemsList = dicService.getDicList(Long.parseLong(id), null);
		} else if(StringUtils.isNotEmpty(dicCode)) {
			itemsList = dicService.getDicList(null, dicCode);
		} else {
			itemsList = dicService.getDicList(1l, null);
		}

		String json = JSON.toJSONString(itemsList);
		super.outString(response, json);

	}
	
	/**
	 * 新增编辑页面根据dicId获取DIC信息
	 */
	@RequestMapping(value = "detail")
	@ResponseBody
	public void dicDetail(Long id, HttpServletRequest req, HttpServletResponse response) {
		try {
			String json = JSON.toJSONString(dicService.getDic(id));
			super.outString(response, json);
		} catch (Exception e) {
			map.put("INFO", e.getMessage());
			super.outString(response, JSON.toJSONString(map));
		}
	}
	
	/**
	 * 获取一级字典项，即字典分组
	 */
	@RequestMapping(value = "topDicData")
	@ResponseBody
	public void topDicData(Dic dic, HttpServletRequest req, HttpServletResponse response) {
		Long nodeId = new Long("1");
		List<Map<String, Object>> itemsList = dicService.getTopDicList(nodeId);

		String json = JSON.toJSONString(itemsList);
		super.outString(response, json);

	}
	
	/**
	 * 根据字典分组获取字典项ComboBox
	 */
	@RequestMapping(value="/dicCombobox/{code}/{mode}", method=RequestMethod.POST)
	public void getDicCmbData(HttpServletResponse response, 
			@PathVariable("code") String code, @PathVariable("mode") String mode) {
		List<Dic> dics = dicService.getDicList4Combox(code, mode);
		
		try {
			this.renderText(response, JSON.toJSONString(dics));
		} catch (IOException e) {
			// XXX
		}
	}
	
	/**
	 * 根据字典ID获取叶子节点
	 */
	@RequestMapping(value="/dicLeaves", method=RequestMethod.POST)
	public @ResponseBody Map<String, String> getDicLeaves(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, String> map = new HashMap<String, String>(1);
		//获取参数
		String id = oConvertUtils.getString(request.getParameter("id"));
		
		List<Dic> result = null;
		try {
			if(StringUtils.isNotEmpty(id)) {
				Dic parmDic = dicService.get(Long.parseLong(id));
				
				if(parmDic!=null && StringUtils.isNotEmpty(parmDic.getPath())) {
					result = dicService.getLeavesByPath(parmDic.getPath());
					if(result!=null && result.size()>0) {
						map.put(AJAX_MESSAGE, "true");
						map.put("data", JSON.toJSONString(result));
					}
				} else {
					map.put(AJAX_MESSAGE, "false");
					map.put(ERR_MESSAGE, "该业务类型子项信息为空");
				}
			} else {
				map.put(AJAX_MESSAGE, "false");
				map.put(ERR_MESSAGE, "参数业务类型ID为空");
			}
		} catch (ApplyException e) {
			e.printStackTrace();
			map.put(AJAX_MESSAGE, "false");
			map.put(ERR_MESSAGE, e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			map.put(AJAX_MESSAGE, "false");
			map.put(ERR_MESSAGE, "获取业务类型信息异常");
		}
		return map;
	}
	
	/**
	 * 根据字典代码获取下级字典信息列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/dicTree", method=RequestMethod.POST)
	public @ResponseBody Map<String, String> getDicTree(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, String> map = new HashMap<String, String>(1);
		//获取参数
		String code = oConvertUtils.getString(request.getParameter("code"));
		List<Dic> dics = dicService.getDicListByCode(code);
		List<Dic> result = null;
		try {
			if(dics!=null && dics.size()>0) {
				Dic parm = dics.get(0);
				result = dicService.getDicListByUpId(parm.getId());
				if(result!=null && result.size()>0) {
					map.put(AJAX_MESSAGE, "true");
					map.put("data", JSON.toJSONString(result));
				} else {
					map.put(AJAX_MESSAGE, "false");
					map.put(ERR_MESSAGE, "该业务类型子项信息为空");
				}
			} else {
				map.put(AJAX_MESSAGE, "false");
				map.put(ERR_MESSAGE, "该业务类型信息为空");
			}
		} catch (ApplyException e) {
			e.printStackTrace();
			map.put(AJAX_MESSAGE, "false");
			map.put(ERR_MESSAGE, e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			map.put(AJAX_MESSAGE, "false");
			map.put(ERR_MESSAGE, "获取业务类型信息异常");
		}
		return map;
	}
}
